package com.liuwei.spring.cloud.controller;

import com.liuwei.spring.cloud.framework.dto.Result;
import com.liuwei.spring.cloud.common.utils.Jackson;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author : MainController
 * @version V1.0
 * @date: 2023/12/23 12:19 AM
 * @Description: TODO
 */
@Slf4j
@RestController
@RequestMapping("/api/redisson")
@Tag(name = "Redisson 工具类")
public class RedissonController {

    @Resource
    RedissonClient redisson;

    @GetMapping("/get")
    public Result<Object> get(@RequestParam("key") String key){
        RBucket<String> bucket = redisson.getBucket(key);
        String value = bucket.get();
        log.info(Jackson.toJSONString(value));
        return Result.success(value);
    }

    @GetMapping("/set")
    public Result<Object> set(@RequestParam("key") String key,@RequestParam("value") String value){
        RBucket<String> bucket = redisson.getBucket(key);
        bucket.set(value);
        value = bucket.get();
        return Result.success(value);
    }

    @GetMapping("/del")
    public Result<Object> del(@RequestParam("key") String key){
        RBucket bucket = redisson.getBucket(key);
        bucket.delete();
        return Result.success();
    }

    @GetMapping("/hget")
    public Result<Object> hget(@RequestParam("hash") String hash,@RequestParam("key") String key){
        RMap<String,String> rmap = redisson.getMap(hash);
        String value = rmap.get(key);
        log.info(Jackson.toJSONString(value));
        return Result.success(value);
    }

    @GetMapping("/hremove")
    public Result<Object> hremove(@RequestParam("hash") String hash,@RequestParam("key") String key){
        RMap<String,String> rmap = redisson.getMap(hash);
        rmap.remove(key);
        return Result.success(rmap.readAllMap());
    }

    @GetMapping("/hset")
    public Result<Object> hset(@RequestParam("hash") String hash,@RequestParam("key") String key,@RequestParam("value") String value){
        RMap<String,String> rmap = redisson.getMap(hash);
        rmap.put(key,value);
        return Result.success(rmap.readAllMap());
    }

    @GetMapping("/lpush")
    public Result<Object> lpush(@RequestParam("list") String list,@RequestParam("key") String key){
        RList<String> bucket = redisson.getList(list);
        bucket.add(0,key);
        return Result.success(bucket.readAll());
    }

    @GetMapping("/lremove")
    public Result<Object> lremove(@RequestParam("list") String list,@RequestParam("key") String key){
        RList<String> bucket = redisson.getList(list);
        bucket.remove(key);
        return Result.success(bucket.readAll());
    }

    @GetMapping("/rpop")
    public Result<Object> rpop(@RequestParam("list") String list){
        RList<String> bucket = redisson.getList(list);
        int size = bucket.size();
        if(size>0){
            bucket.remove(size-1);
        }
        return Result.success(bucket.readAll());
    }

    @GetMapping("/sadd")
    public Result<Object> sadd(@RequestParam("set") String set,@RequestParam("key") String key){
        RSet<String> bucket = redisson.getSet(set);
        bucket.add(key);
        return Result.success(bucket.readAll());
    }

    @GetMapping("/spop")
    public Result<Object> spop(@RequestParam("set") String set,@RequestParam("key") String key){
        RSet<String> bucket = redisson.getSet(set);
        bucket.remove(key);
        return Result.success(bucket.readAll());
    }

    @GetMapping("/zadd")
    public Result<Object> zadd(@RequestParam("zset") String zset,@RequestParam("key") String key,@RequestParam("score") double score){
        RScoredSortedSet<String> bucket = redisson.getScoredSortedSet(zset);
        bucket.add(score,key);
        return Result.success(bucket.readAll());
    }

    @GetMapping("/zrem")
    public Result<Object> zrem(@RequestParam("zset") String zset,@RequestParam("key") String key){
        RScoredSortedSet<String> bucket = redisson.getScoredSortedSet(zset);
        bucket.remove(key);
        return Result.success(bucket.readAll());
    }

    @GetMapping("/bigkeys")
    public Result<?> bigkeys(){
        RKeys rkeys = redisson.getKeys();
        Map<String,String> allKeys = new HashMap<>();
        Iterator<String> iterator = rkeys.getKeysByPattern(null,2).iterator();
        while(iterator.hasNext()){
            String key = iterator.next();
            RType type = rkeys.getType(key);
            allKeys.put(key,type.name());
            if(RType.OBJECT.equals(type)){
                RBucket<?> bucket = redisson.getBucket(key);
                //Returns size of object in bytes
                bucket.size();
            }
            if(RType.LIST.equals(type)){
                RList<?> list = redisson.getList(key);
                //the number of elements in this list
                list.size();
            }
        }
        return Result.success(allKeys);
    }
}
